package cn.ylz.ylmall.account.dao.persist.repository.impl;

import cn.ylz.ylmall.account.dao.persist.mapper.UserMapper;
import cn.ylz.ylmall.account.dao.persist.repository.IUserRepository;
import cn.ylz.ylmall.account.pojo.entity.User;
import cn.ylz.ylmall.account.pojo.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.ylz.ylmall.common.util.PageInfoToPageDataConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class UserRepositoryImpl implements IUserRepository {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int countByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhone(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmail(String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public UserLoginInfoVO selectByUsername(String s) {
        UserLoginInfoVO userLoginInfoVO = userMapper.selectByUsername(s);
        return userLoginInfoVO;
    }

    @Override
    public int insert(User user) {
        int count = userMapper.insert(user);
        return count;
    }

    @Override
    public PageData<UserListItemVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserListItemVO> list = userMapper.list();
        PageInfo<UserListItemVO> pageInfo = new PageInfo<>(list);
        PageData<UserListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public int delete(Long id) {
        return userMapper.deleteById(id);
    }

    @Override
    public UserStandardVO getStandardById(Long id) {
        return userMapper.getStandardById(id);
    }

    @Override
    public int update(User user) {
        int rows = userMapper.updateById(user);
        return rows;
    }

    @Override
    public List<UserPermissionVO> PermissionsById(Long id) {
        return userMapper.PermissionsById(id);
    }

    @Override
    public List<UserRoleVO> roleById(Long userId) {
        return userMapper.roleById(userId);
    }

    @Override
    public PageData<UserListItemVO> listAdmin(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserListItemVO> list = userMapper.listAdmin();
        PageInfo<UserListItemVO> pageInfo = new PageInfo<>(list);
        PageData<UserListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
